package doorheenkijker;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class KijkPanel extends JPanel {

  public static BufferedImage koekje1 = null;
  public static BufferedImage koekje2 = null;
  int x = 0;
  int y = 0;
  public static double hor = 100;
  public static double vert = 100;
  //boolean bestandLatenKiezen = false;

  public KijkPanel() {
    //this.setPreferredSize(new Dimension(640,480));
    //if (bestandLatenKiezen) {
    /*
    File f1 = openImageBestand();
    if (f1 == null) {
    System.exit(0);
    }
    File f2 = openImageBestand();
    if (f2 == null) {
    System.exit(0);
    }
    try {
    koekje1 = ImageIO.read(f1);
    koekje2 = ImageIO.read(f2);
    } catch (Exception ex) {
    ex.printStackTrace();
    System.err.println("Programma wordt afgesloten, ivm een Exception...");
    System.exit(0);
    }*/
    //} else {
    try {
      koekje1 = ImageIO.read(this.getClass().getResource("nr1.png"));
      koekje2 = ImageIO.read(this.getClass().getResource("nr2.png"));
    } catch (Exception ex) {
      ex.printStackTrace();
      System.err.println("Programma wordt afgesloten, ivm een Exception...");
      System.exit(0);
    }
    //}
    this.addMouseMotionListener(new MouseMotionAdapter() {

      @Override
      public void mouseMoved(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        doeRepaint();
      }
    });

  }

  public void doeRepaint() {
    this.repaint();
  }

  @Override
  public void paintComponent(Graphics g) {
    g.clearRect(0, 0, this.getWidth(), this.getHeight());
    g.drawImage(koekje1, 0, 0, null); //Voorgrond //ALS FRAME & Plaatje 640 is.
    //g.drawImage(koekje1, 0, 0, this.getWidth(), this.getHeight(), null); //Voorgrond //WERKT ALTIJD.
    g.drawImage(koekje2, (int) (x - (hor / 2)), (int) (y - (vert / 2)), (int) (x + (hor / 2)), (int) (y + (vert / 2)), (int) (x - (hor / 2)), (int) (y - (vert / 2)), (int) (x + (hor / 2)), (int) (y + (vert / 2)), null); //Achtergrond // Als Frame & plaatje 640 is.
    //g.drawImage(koekje1,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,null);
    g.drawRect((int) (x - (hor / 2)), (int) (y - (vert / 2)), (int) hor, (int) vert);
  }
}
